package com.jbacon.everadiodownloader.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatcher {
    private static final String FILE_EXTENSION = ".mp3";
    private static final String COVER_SHOW = "[Cover]";
    private static final String SPLIT_ON = "_";
    private static final String DATE_SEPARATOR = "-";
    private static final String REGEX_REWIND_PAGE = "recidx=([0-9]*&station=[A-Z]*)";
    private static final String REGEX_SHOW_NAMES = "<div id='dj'>[^<]*</div><div>[^<]*</div><div>[^<]*</div>";
    private static final String REGEX_PLAYLIST = "embed type.*pluginspage.*src.{2}(.*m3u).{1}";
    private static final String REGEX_MP3 = "http://[^#, ]*\\.mp3";

    public static final Pattern PATTERN_REWIND_PAGE = Pattern.compile(REGEX_REWIND_PAGE);
    public static final Pattern PATTERN_SHOW_NAMES = Pattern.compile(REGEX_SHOW_NAMES);
    public static final Pattern PATTERN_PLAYLIST = Pattern.compile(REGEX_PLAYLIST);
    public static final Pattern PATTERN_MP3 = Pattern.compile(REGEX_MP3);


    public List<String> getAllMatchning(final Pattern pattern, final String source) {
        if (pattern.equals(PATTERN_REWIND_PAGE)) {
            return getAllMatchning(pattern, source, 1);
        }
        if (pattern.equals(PATTERN_PLAYLIST)) {
            return getAllMatchning(pattern, source, 1);
        }
        if (pattern.equals(PATTERN_SHOW_NAMES)) {
            return getAllMatchning(pattern, source, 0);
        }
        if (pattern.equals(PATTERN_MP3)) {
            return getAllMatchning(pattern, source, 0);
        }
        return Collections.emptyList();
    }


    public List<String> replaceAll(final String toReplace, final String toReplaceWith, final List<String> sources) {
        final List<String> result = new ArrayList<String>();
        for (final String source : sources) {
            result.add(replace(toReplace, toReplaceWith, source));
        }
        return result;
    }


    public List<String> reorderAllShowNames(final List<String> showNamesToReorder) {
        final List<String> reorderedShowNames = new ArrayList<String>();
        for (final String showName : showNamesToReorder) {
            reorderedShowNames.add(reorderShowName(showName));
        }
        return reorderedShowNames;
    }


    private String reorderShowName(final String showNameToReorder) {
        final String[] splitString = showNameToReorder.split(SPLIT_ON);
        final StringBuilder reorderedString = new StringBuilder();

        int i = 0;
        reorderedString.append(splitString[i]);

        if (COVER_SHOW.equalsIgnoreCase(splitString[1])) {
            reorderedString.append(SPLIT_ON); // _
            reorderedString.append(splitString[i + 1]); // [Cover]
            i++;
        }
        reorderedString.append(SPLIT_ON); // _
        reorderedString.append(splitString[i + 6]); // 2011
        reorderedString.append(DATE_SEPARATOR); // _
        reorderedString.append(convertMonth(splitString[i + 5])); // September
        reorderedString.append(DATE_SEPARATOR); // _
        reorderedString.append(convertDay(splitString[i + 3])); // 25th
        reorderedString.append(DATE_SEPARATOR); // _
        reorderedString.append(splitString[i + 2]); // 0600
        reorderedString.append(DATE_SEPARATOR); // _
        reorderedString.append(splitString[i + 1]); // Sunday
        reorderedString.append(FILE_EXTENSION); // .mp3

        return reorderedString.toString();
    }


    private String convertDay(final String day) {
        final String finalDay = day.substring(0, day.length() - 2);
        if (finalDay.length() == 1) {
            return "0" + finalDay;
        }
        return finalDay;
    }


    private String convertMonth(final String month) {
        if ("January".equalsIgnoreCase(month)) {
            return "01";
        }
        if ("February".equalsIgnoreCase(month)) {
            return "02";
        }
        if ("March".equalsIgnoreCase(month)) {
            return "03";
        }
        if ("April".equalsIgnoreCase(month)) {
            return "04";
        }
        if ("May".equalsIgnoreCase(month)) {
            return "05";
        }
        if ("June".equalsIgnoreCase(month)) {
            return "06";
        }
        if ("July".equalsIgnoreCase(month)) {
            return "07";
        }
        if ("August".equalsIgnoreCase(month)) {
            return "08";
        }
        if ("September".equalsIgnoreCase(month)) {
            return "09";
        }
        if ("October".equalsIgnoreCase(month)) {
            return "10";
        }
        if ("November".equalsIgnoreCase(month)) {
            return "11";
        }
        if ("December".equalsIgnoreCase(month)) {
            return "12";
        }
        return month;
    }


    private String replace(final String toReplace, final String toReplaceWith, final String source) {
        return source.replace(toReplace, toReplaceWith);
    }


    private List<String> getAllMatchning(final Pattern pattern, final String source, final int groupNumber) {
        final Matcher matcher = pattern.matcher(source);
        final List<String> foundMatches = new ArrayList<String>();

        while (matcher.find()) {
            foundMatches.add(matcher.group(groupNumber));
        }
        return foundMatches;
    }
}
